package vistas;

import interfaz.TDAManejoDatos;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.SwingUtilities;

import util.FechaUtil;

import controller.ControllerPrincipal;

import beans.CotizacionBean;
import beans.EntidadBean;
import beans.OrdenDePedidoBean;
import beans.ReferenciaCotizacionBean;
import beans.ReferenciaCotizacionRodamientos;
import beans.RodamientoBean;
import beans.SolicitudCotizacionBean;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class GenerarOP extends BaseJFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7030896646166961016L;

	private JButton buttonSalir;
	private JButton buttonModificar;
	private JLabel jLabel6;
	private JButton jButton2;
	private JList jList1;
	private DefaultListModel listModel = new DefaultListModel();
	private List<CotizacionBean>  lista;

	public GenerarOP() {
		
		super();
		initGUI();
	}

	private void initGUI() {
		
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Generar Orden de Pedido" + "");
			 


			{
				
				buttonSalir = new JButton();
				getContentPane().add(buttonSalir);
				buttonSalir.setText("Salir");
				buttonSalir.setBounds(20, 249, 111, 23);
				buttonSalir.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						_exit();
					}
				});
			}
			{
				jList1 = new JList(listModel);
				jList1
						.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				jList1.setLayoutOrientation(JList.VERTICAL);
				jList1.setVisibleRowCount(-1);
				ControllerPrincipal controller = ControllerPrincipal.getInstancia();
				lista=controller.getCotizacionesPendientes();
				
				for(CotizacionBean co: lista){
					listModel.addElement(co);
				}
				JScrollPane listScroller = new JScrollPane(jList1);
				getContentPane().add(jList1);
				jList1.setBounds(20, 62, 612, 143);
			}
			{
				
				jButton2 = new JButton();
				getContentPane().add(jButton2);
				jButton2.setText("Generar Orden de Pedido");
				jButton2.setBounds(360, 217, 216, 23);
				jButton2.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						//jButton2ActionPerformed(evt);
						
						OrdenDePedidoBean op = new OrdenDePedidoBean();
						ReferenciaCotizacionBean ref = new ReferenciaCotizacionBean();
						CotizacionBean a= lista.get(jList1.getSelectedIndex());   
						//(CotizacionBean) listModel.getElementAt(jList1.getSelectedIndex());
						ReferenciaCotizacionRodamientos rcr = new ReferenciaCotizacionRodamientos();
						rcr.setNumero(a.getNroCotizacion());
						rcr.setCondicionesDeVenta(a.getCondicionesVenta());
						rcr.setFecha(FechaUtil.getStringFromDate(FechaUtil.getHoy()));
						rcr.setRodamientos(a.getReferenciaSolicitudCotizacion().getRodamientos());
						ref.setCotizacion(rcr);
						op.setReferencia(ref);
						op.setCliente(a.getCliente());
						op.setFecha(FechaUtil.getStringFromDate(FechaUtil.getHoy()));
						ControllerPrincipal controller = ControllerPrincipal.getInstancia();
						try {
							controller.cargarOrdenPedido(op);
							JOptionPane.showMessageDialog(new JFrame(), "Se generaron exitosamente las ODP, Las ODC, REMITOS y FACTURAS CORRESPONDIENTES");
							_exit();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				jLabel6 = new JLabel();
				getContentPane().add(jLabel6);
				jLabel6.setText("Cotizaciones Pendientes:");
				jLabel6.setBounds(187, 34, 181, 16);
			}

			pack();
			this.setSize(671, 322);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void _exit() {
		this.dispose();
	}
	
	private void jButton1ActionPerformed(ActionEvent evt) {
		System.out.println("jButton1.actionPerformed, event="+evt);
		//TODO add your code for jButton1.actionPerformed
	}
	
	private void jButton2ActionPerformed(ActionEvent evt) {
		System.out.println("jButton2.actionPerformed, event="+evt);
		//TODO add your code for jButton2.actionPerformed
	}
}
